package cn.bgw.springBeanTest;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

public class Person implements BeanFactoryAware, BeanNameAware,
        InitializingBean, DisposableBean {

    private String name;
    private String address;
    private int phone;

    private BeanFactory beanFactory;
    private String beanName;

    public Person() {
        System.out.println("【构造器】调用Person的构造器实例化");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("【注入属性】注入属性name");
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        System.out.println("【注入属性】注入属性address");
        this.address = address;
    }

    public int getPhone() {
        return phone;
    }

    public void setPhone(int phone) {
        System.out.println("【注入属性】注入属性phone");
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Person [address=" + address + ", name=" + name + ", phone="
                + phone + "]";
    }

    // 这是BeanFactoryAware接口方法
    @Override
    public void setBeanFactory(BeanFactory arg0) throws BeansException {
        System.out
                .println("【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()");
        this.beanFactory = arg0;
    }

    // 这是BeanNameAware接口方法
    @Override
    public void setBeanName(String arg0) {
        System.out.println("【BeanNameAware接口】调用BeanNameAware.setBeanName()");
        this.beanName = arg0;
    }

    // 这是InitializingBean接口方法
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out
                .println("【InitializingBean接口】调用InitializingBean.afterPropertiesSet()");
    }

    // 这是DiposibleBean接口方法
    @Override
    public void destroy() throws Exception {
        System.out.println("【DiposibleBean接口】调用DiposibleBean.destory()");
    }

    // 通过<bean>的init-method属性指定的初始化方法
    public void myInit() {
        System.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法");
    }

    // 通过<bean>的destroy-method属性指定的初始化方法
    public void myDestory() {
        System.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
    }

    // 现在开始初始化容器
    // 2014-5-18 15:46:20 org.springframework.context.support.AbstractApplicationContext prepareRefresh
    // 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@19a0c7c: startup date [
    // Sun May 18 15:46:20 CST 2014]; root of context hierarchy
    // 2014-5-18 15:46:20 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    // 信息: Loading XML bean definitions from class path resource [cn.bgw.springBeanTest/beans.xml]
    // 这是BeanFactoryPostProcessor实现类构造器！！
    // BeanFactoryPostProcessor调用postProcessBeanFactory方法
    // 这是BeanPostProcessor实现类构造器！！
    // 这是InstantiationAwareBeanPostProcessorAdapter实现类构造器！！
    //         2014-5-18 15:46:20 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    // 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9934d4: defining beans [beanPostProcessor,instantiationAwareBeanPostProcessor,beanFactoryPostProcessor,person]; root of factory hierarchy
    // InstantiationAwareBeanPostProcessor调用postProcessBeforeInstantiation方法
    // 【构造器】调用Person的构造器实例化
    //         InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法
    // 【注入属性】注入属性address
    // 【注入属性】注入属性name
    // 【注入属性】注入属性phone
    // 【BeanNameAware接口】调用BeanNameAware.setBeanName()
    //         【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()
    // BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改！
    //         【InitializingBean接口】调用InitializingBean.afterPropertiesSet()
    //         【init-method】调用<bean>的init-method属性指定的初始化方法
    // BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改！
    // InstantiationAwareBeanPostProcessor调用postProcessAfterInitialization方法
    //         容器初始化成功
    // Person [address=广州, name=张三, phone=110]
    // 现在开始关闭容器！
    //         【DiposibleBean接口】调用DiposibleBean.destory()
    //         【destroy-method】调用<bean>的destroy-method属性指定的初始化方法
}
